//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2005
//
// Set end level attribute for object.
//

# include "StdAfx.h"
# include "SetRgnLevels.h"
# include "UndoRedo.h"
# include "Label.h"
# include "Globals.h"

class CSetObjectEndLevelAction : public CEditAction {
  public:
	CSetObjectEndLevelAction (rgn_t & _rgn, size_t _cEndLevel) :
		m_rgn (_rgn),
		m_cNewEndLevel (_cEndLevel),
		m_cOldEndLevel (_rgn.uchEndLevel),
		m_bWasModified ((_rgn.dwFlags & rgn_t::fModified) != 0)
	{}

	virtual void GetName (string_t & _str) const;
	virtual cu_t CanUndo () const {return cuYes;}
	virtual bool Do ();
	virtual void Undo ();
	virtual void Redo () {CSetObjectEndLevelAction::Do ();}

  private:
	rgn_t &      m_rgn;
	size_t const m_cNewEndLevel;
	size_t const m_cOldEndLevel;
	bool   const m_bWasModified;
};

void CSetObjectEndLevelAction::GetName (string_t & _str) const {
	CCritSec::CLock lock (g_cs);

	_str.append ("set end level for [");
	GetHintText (m_rgn, _str);
	_str.append ("]");
}

bool CSetObjectEndLevelAction::Do () {
	CCritSec::CLock lock (g_cs);

	assert (m_cNewEndLevel < 255);
	m_rgn.uchEndLevel = m_cNewEndLevel;

	m_rgn.dwFlags |= rgn_t::fModified;

	return true;
}

void CSetObjectEndLevelAction::Undo () {
	CCritSec::CLock lock (g_cs);

	m_rgn.uchEndLevel = m_cOldEndLevel;

	if (! m_bWasModified)
		m_rgn.dwFlags &= ~rgn_t::fModified;
}

/////////////////////////////////////////////////////////////////

void SetObjectEndLevel (rgn_t & _rgn, size_t _cEndLevel) {
	assert (! _rgn.IsWTR ());
	PerformEditAction (new CSetObjectEndLevelAction (_rgn, _cEndLevel));
}
